#include "LoginScene.h"
#include "cocos-ext.h"
#include "PostMsg.h"
#include "CPublicFuns.h"
CCScene* CLoginScene::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	CLoginScene *layer = CLoginScene::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool CLoginScene::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}

	m_visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	m_origin = CCDirector::sharedDirector()->getVisibleOrigin();

	/////////////////////////////
	// 2. add a menu item with "X" image, which is clicked to quit the program
	//    you may modify it.

	// add a "close" icon to exit the progress. it's an autorelease object

	CreateCloseBtn();
	CreateLoginBtn();
	/////////////////////////////
	// 3. add your codes below...

	// add a label shows "Hello World"
	// create and initialize a label

	CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

	// position the label on the center of the screen
	pLabel->setPosition(ccp(m_origin.x + m_visibleSize.width/2,
		m_origin.y + m_visibleSize.height - pLabel->getContentSize().height));

	// add the label as a child to this layer
	this->addChild(pLabel, 1);

	// add "HelloWorld" splash screen"
	CCSprite* pSprite = CCSprite::create("HelloWorld.png");

	// position the sprite on the center of the screen
	pSprite->setPosition(ccp(m_visibleSize.width/2 + m_origin.x, m_visibleSize.height/2 + m_origin.y));

	// add the sprite as a child to this layer
	this->addChild(pSprite, 0);

	return true;
}



void CLoginScene::Callback_OnBtnClickClose(CCObject* pSender)
{
	char* pBuf = "123|456|qqq|1";
	vector<CCString*> verChar;
	CCPublicFuns::SplitString(pBuf,verChar);

	for (int i=0; i<verChar.size();i++)
	{
		CCMessageBox(verChar[i]->getCString(),"test");
	}

	char* pJson = "{\"key1\":\"Value1\"}";
	CCMessageBox(CCPublicFuns::GetJsonKey(pJson)->getCString(),"test");
	CCMessageBox(CCPublicFuns::GetJsonValue(pJson)->getCString(),"test");


#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
#endif
}

void CLoginScene::CreateCloseBtn()
{
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		this,
		menu_selector(CLoginScene::Callback_OnBtnClickClose));

	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
		origin.y + pCloseItem->getContentSize().height/2));

	// create menu, it's an autorelease object
	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);
}

void CLoginScene::CreateLoginBtn()
{
	CCMenuItemImage * pItem = CreateBtnTemplate(menu_selector(CLoginScene::Callback_OnBtnClickLogin));
	pItem->setPosition(ccp(m_origin.x + m_visibleSize.width/2 ,
		m_origin.y + pItem->getContentSize().height/2));
	//pItem->set
}

void CLoginScene::CreateRegisterBtn()
{
// 	Button *b = Button::create("animationbuttonnormal.png","animationbuttonpressed.png");
// 
// 	b->addTouchEventListener(CC_CALLBACK_2(GuiBotton::touchEvent,this));
// 	b->setPosition(ccp(widgetSize.width*0.8,widgetSize.height*0.2));
// 	b->setPressedActionEnabled(true);
// 	b->setColor(Color3B::RED);
// 	b->setOpacity(100);
// 	b->setTitleText("onclick me");
// 	addChild(b);
}

void CLoginScene::Callback_OnBtnClickLogin( CCObject* pSender )
{
	CPostMsg post;
	post.PostLoginCheck("xxxr201209","12345678");
	CCMessageBox(post.GetResult().getCString(),"LoginResult");
}

void CLoginScene::Callback_OnBtnClickRegister( CCObject* pSender )
{

}

CCMenuItemImage * CLoginScene::CreateBtnTemplate( SEL_MenuHandler fun)
{
	CCMenuItemImage *pItem = CCMenuItemImage::create(
		"button_n.png",
		"button_p.png",
		this,
		fun);

	pItem->setPosition(ccp(m_origin.x + m_visibleSize.width - pItem->getContentSize().width/2 ,
		m_origin.y + pItem->getContentSize().height/2));

	// create menu, it's an autorelease object
	CCMenu* pMenu = CCMenu::create(pItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

	return pItem;
}


void CLoginScene::onHttpRequestCompleted(cocos2d::CCNode *sender ,void *data)  
{  
//  	AllocConsole();  
// 	freopen("CONIN$", "r", stdin);  
// 	freopen("CONOUT$", "w", stdout);  
// 	freopen("CONOUT$", "w", stderr);  
// 	cocos2d::extension::CCHttpResponse *response = (cocos2d::extension::CCHttpResponse*)data;    
// 	if (!response)    
// 	{  
// 		return;    
// 	}   
// 	if (0 != strlen(response->getHttpRequest()->getTag()))   
// 	{  
// 		CCLog("%s completed", response->getHttpRequest()->getTag());  
// 	}  
//  	int statusCode = response->getResponseCode();  
// // 	char statusString[64] = {};  
// // 	sprintf(statusString ,"Http status code:%d ,tag = %s" ,statusCode ,response->getHttpRequest()->getTag());  
// // 	CCLog("response code:%d" ,statusCode);  
// 	if (!response->isSucceed())  
// 	{  
// 		CCLog("response failed");  
// 		CCLog("error buffer:%s" ,response->getErrorBuffer());  
// 	}  
// 	std::vector</*<span class="datatypes">*/char> *buffer = response->getResponseData(); 
// 	//printf("Http response,dump data:");  
// 	std::string result = "";  
// 	for (unsigned int i = 0; i < buffer->size(); i ++)  
// 	{  
// 		printf("%c" ,(*buffer)[i]);  
// 	}  
}  